package com.marketapp.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.marketapp.domain.Equity;
import com.marketapp.domain.Sector;
import com.marketapp.helper.ServiceException;
import com.marketapp.service.IEquityService;
import com.marketapp.service.ISectorService;
import com.marketapp.util.StringUtil;
import com.marketapp.validators.EquityValidator;

@Controller
@RequestMapping(value = "/admin_add_equity")
public class AdminAddEquityController {

	@Autowired
	IEquityService equityService;

	@Autowired
	ISectorService sectorService;

	@Autowired
	EquityValidator equityValidator;
	

	public EquityValidator getEquityValidator() {
		return equityValidator;
	}

	public void setEquityValidator(EquityValidator equityValidator) {
		this.equityValidator = equityValidator;
	}

	public IEquityService getEquityService() {
		return equityService;
	}

	public void setEquityService(IEquityService equityService) {
		this.equityService = equityService;
	}

	@RequestMapping(method = RequestMethod.GET)
	public void setupForm(Model model) {
		Equity equity = new Equity();
		Sector sector = new Sector();
		equity.setEquitySector(sector);
		model.addAttribute("equity", equity);
	}

	@RequestMapping(method = RequestMethod.POST)
	public String saveData(@ModelAttribute(value = "equity") Equity equity,
			BindingResult result, Model model) throws ServiceException {
		equityValidator.validate(equity, result);
		if (result.hasErrors()) {
			model.addAttribute("successFlag", false);
			return "admin_add_equity";
		}
		equity.setEquitySector(sectorService.getSectorById(equity
				.getEquitySector().getSectorId()));
		equityService.saveEquity(equity);
		model.addAttribute("successFlag", true);
		return "admin_add_equity";
	}

	@ModelAttribute("sectorDD")
	public List<Sector> populateSectorDD() throws ServiceException {
		return sectorService.getAllSector();
	}

	public ISectorService getSectorService() {
		return sectorService;
	}

	public void setSectorService(ISectorService sectorService) {
		this.sectorService = sectorService;
	}

	/*
	 * @RequestMapping(value = "/getSectorList", method = RequestMethod.GET)
	 * public @ResponseBody List<String> getSectorList(@RequestParam("term")
	 * String query) throws ServiceException { return
	 * sectorService.searchSectorByName(query); }
	 */
}
